home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / pyatspi / utils.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  18.9 KB  |  590 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''
  5. Utility functions for AT-SPI for querying interfaces, searching the hierarchy,
  6. converting constants to strings, and so forth.
  7.  
  8. @author: Peter Parente
  9. @organization: IBM Corporation
  10. @copyright: Copyright (c) 2005, 2007 IBM Corporation
  11. @license: LGPL
  12.  
  13. This library is free software; you can redistribute it and/or
  14. modify it under the terms of the GNU Library General Public
  15. License as published by the Free Software Foundation; either
  16. version 2 of the License, or (at your option) any later version.
  17.  
  18. This library is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  21. Library General Public License for more details.
  22.  
  23. You should have received a copy of the GNU Library General Public
  24. License along with this library; if not, write to the
  25. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  26. Boston, MA 02111-1307, USA.
  27.  
  28. Portions of this code originally licensed and copyright (c) 2005, 2007
  29. IBM Corporation under the BSD license, available at
  30. U{http://www.opensource.org/licenses/bsd-license.php}
  31. '''
  32. import ORBit
  33. import Accessibility__POA
  34.  
  35. def getInterfaceIID(obj):
  36.     '''
  37.   Gets the ID of an interface class or object in string format for use in
  38.   queryInterface.
  39.   
  40.   @param obj: Class representing an AT-SPI interface or instance
  41.   @type obj: object
  42.   @return: IID for the interface
  43.   @rtype: string
  44.   @raise AttributeError: When the parameter does not provide typecode info
  45.   '''
  46.     return obj.__typecode__.repo_id
  47.  
  48.  
  49. def getInterfaceName(obj):
  50.     '''
  51.   Gets the human readable name of an interface class or object in string
  52.   format.
  53.   
  54.   @param obj: Class representing an AT-SPI interface or instance
  55.   @type obj: class
  56.   @return: Name of the interface
  57.   @rtype: string
  58.   @raise AttributeError: When the parameter does not provide typecode info
  59.   '''
  60.     return obj.__typecode__.name
  61.  
  62. import constants
  63.  
  64. def listInterfaces(obj):
  65.     '''
  66.   Gets a list of the names of all interfaces supported by this object. The
  67.   names are the short-hand interface names like "Accessible" and "Component",
  68.   not the full interface identifiers.
  69.  
  70.   @param obj: Arbitrary object to query for all accessibility related
  71.   interfaces. Must provide a queryInterface method.
  72.   @type obj: object
  73.   @return: Set of supported interface names
  74.   @rtype: set
  75.   @raise AttributeError: If the object provide does not implement
  76.   queryInterface
  77.   '''
  78.     names = set()
  79.     for ic in constants.ALL_INTERFACES:
  80.         io = obj.queryInterface(getInterfaceIID(ic))
  81.         if io is None:
  82.             continue
  83.         
  84.         names.add(getInterfaceName(ic))
  85.     
  86.     return names
  87.  
  88.  
  89. def stringToConst(prefix, suffix):
  90.     '''
  91.   Maps a string name to an AT-SPI constant. The rules for the mapping are as 
  92.   follows:
  93.     - The prefix is captalized and has an _ appended to it.
  94.     - All spaces in the suffix are mapped to the _ character. 
  95.     - All alpha characters in the suffix are mapped to their uppercase.
  96.     
  97.   The resulting name is used with getattr to look up a constant with that name
  98.   in the L{constants} module. If such a constant does not exist, the string
  99.   suffix is returned instead.
  100.  
  101.   This method allows strings to be used to refer to roles, relations, etc.
  102.   without direct access to the constants. It also supports the future expansion
  103.   of roles, relations, etc. by allowing arbitrary strings which may or may not
  104.   map to the current standard set of roles, relations, etc., but may still
  105.   match some non-standard role, relation, etc. being reported by an
  106.   application.
  107.   
  108.   @param prefix: Prefix of the constant name such as role, relation, state, 
  109.     text, modifier, key
  110.   @type prefix: string
  111.   @param suffix: Name of the role, relation, etc. to use to lookup the constant
  112.   @type suffix: string
  113.   @return: The matching constant value
  114.   @rtype: object
  115.   '''
  116.     name = prefix.upper() + '_' + suffix.upper().replace(' ', '_')
  117.     return getattr(constants, name, suffix)
  118.  
  119.  
  120. def stateToString(value):
  121.     '''
  122.   Converts a state value to a string based on the name of the state constant in
  123.   the L{constants} module that has the given value.
  124.   
  125.   @param value: An AT-SPI state
  126.   @type value: Accessibility.StateType
  127.   @return: Human readable, untranslated name of the state
  128.   @rtype: string
  129.   '''
  130.     return constants.STATE_VALUE_TO_NAME.get(value)
  131.  
  132.  
  133. def relationToString(value):
  134.     '''
  135.   Converts a relation value to a string based on the name of the state constant
  136.   in the L{constants} module that has the given value.
  137.   
  138.   @param value: An AT-SPI relation
  139.   @type value: Accessibility.RelationType
  140.   @return: Human readable, untranslated name of the relation
  141.   @rtype: string
  142.   '''
  143.     return constants.RELATION_VALUE_TO_NAME.get(value)
  144.  
  145.  
  146. def allModifiers():
  147.     '''
  148.   Generates all possible keyboard modifiers for use with 
  149.   L{registry.Registry.registerKeystrokeListener}.
  150.   '''
  151.     mask = 0
  152.     while mask <= 1 << constants.MODIFIER_NUMLOCK:
  153.         yield mask
  154.         mask += 1
  155.  
  156.  
  157. def findDescendant(acc, pred, breadth_first = False):
  158.     """
  159.   Searches for a descendant node satisfying the given predicate starting at 
  160.   this node. The search is performed in depth-first order by default or
  161.   in breadth first order if breadth_first is True. For example,
  162.   
  163.   my_win = findDescendant(lambda x: x.name == 'My Window')
  164.   
  165.   will search all descendants of x until one is located with the name 'My
  166.   Window' or all nodes are exausted. Calls L{_findDescendantDepth} or
  167.   L{_findDescendantBreadth} to start the recursive search.
  168.   
  169.   @param acc: Root accessible of the search
  170.   @type acc: Accessibility.Accessible
  171.   @param pred: Search predicate returning True if accessible matches the 
  172.       search criteria or False otherwise
  173.   @type pred: callable
  174.   @param breadth_first: Search breadth first (True) or depth first (False)?
  175.   @type breadth_first: boolean
  176.   @return: Accessible matching the criteria or None if not found
  177.   @rtype: Accessibility.Accessible or None
  178.   """
  179.     if breadth_first:
  180.         return _findDescendantBreadth(acc, pred)
  181.     for child in acc:
  182.         
  183.         try:
  184.             ret = _findDescendantDepth(acc, pred)
  185.         except Exception:
  186.             breadth_first
  187.             breadth_first
  188.             ret = None
  189.         except:
  190.             breadth_first
  191.  
  192.         if ret is not None:
  193.             return ret
  194.     
  195.  
  196.  
  197. def _findDescendantBreadth(acc, pred):
  198.     '''    
  199.   Internal function for locating one descendant. Called by L{findDescendant} to
  200.   start the search.
  201.   
  202.   @param acc: Root accessible of the search
  203.   @type acc: Accessibility.Accessible
  204.   @param pred: Search predicate returning True if accessible matches the 
  205.       search criteria or False otherwise
  206.   @type pred: callable
  207.   @return: Matching node or None to keep searching
  208.   @rtype: Accessibility.Accessible or None
  209.   '''
  210.     for child in acc:
  211.         
  212.         try:
  213.             if pred(child):
  214.                 return child
  215.         continue
  216.         except Exception:
  217.             continue
  218.         
  219.  
  220.     
  221.     for child in acc:
  222.         
  223.         try:
  224.             ret = _findDescendantBreadth(child, pred)
  225.         except Exception:
  226.             None<EXCEPTION MATCH>Exception
  227.             None<EXCEPTION MATCH>Exception
  228.             ret = None
  229.         except:
  230.             None<EXCEPTION MATCH>Exception
  231.  
  232.         if ret is not None:
  233.             return ret
  234.     
  235.  
  236.  
  237. def _findDescendantDepth(acc, pred):
  238.     '''
  239.   Internal function for locating one descendant. Called by L{findDescendant} to
  240.   start the search.
  241.  
  242.   @param acc: Root accessible of the search
  243.   @type acc: Accessibility.Accessible
  244.   @param pred: Search predicate returning True if accessible matches the 
  245.     search criteria or False otherwise
  246.   @type pred: callable
  247.   @return: Matching node or None to keep searching
  248.   @rtype: Accessibility.Accessible or None
  249.   '''
  250.     
  251.     try:
  252.         if pred(acc):
  253.             return acc
  254.     except Exception:
  255.         pass
  256.  
  257.     for child in acc:
  258.         
  259.         try:
  260.             ret = _findDescendantDepth(child, pred)
  261.         except Exception:
  262.             ret = None
  263.  
  264.         if ret is not None:
  265.             return ret
  266.     
  267.  
  268.  
  269. def findAllDescendants(acc, pred):
  270.     '''
  271.   Searches for all descendant nodes satisfying the given predicate starting at 
  272.   this node. Does an in-order traversal. For example,
  273.   
  274.   pred = lambda x: x.getRole() == pyatspi.ROLE_PUSH_BUTTON
  275.   buttons = pyatspi.findAllDescendants(node, pred)
  276.   
  277.   will locate all push button descendants of node.
  278.   
  279.   @param acc: Root accessible of the search
  280.   @type acc: Accessibility.Accessible
  281.   @param pred: Search predicate returning True if accessible matches the 
  282.       search criteria or False otherwise
  283.   @type pred: callable
  284.   @return: All nodes matching the search criteria
  285.   @rtype: list
  286.   '''
  287.     matches = []
  288.     _findAllDescendants(acc, pred, matches)
  289.     return matches
  290.  
  291.  
  292. def _findAllDescendants(acc, pred, matches):
  293.     '''
  294.   Internal method for collecting all descendants. Reuses the same matches
  295.   list so a new one does not need to be built on each recursive step.
  296.   '''
  297.     for child in acc:
  298.         
  299.         try:
  300.             if pred(child):
  301.                 matches.append(child)
  302.         except Exception:
  303.             pass
  304.  
  305.         _findAllDescendants(child, pred, matches)
  306.     
  307.  
  308.  
  309. def findAncestor(acc, pred):
  310.     '''
  311.   Searches for an ancestor satisfying the given predicate. Note that the
  312.   AT-SPI hierarchy is not always doubly linked. Node A may consider node B its
  313.   child, but B is not guaranteed to have node A as its parent (i.e. its parent
  314.   may be set to None). This means some searches may never make it all the way
  315.   up the hierarchy to the desktop level.
  316.   
  317.   @param acc: Starting accessible object
  318.   @type acc: Accessibility.Accessible
  319.   @param pred: Search predicate returning True if accessible matches the 
  320.     search criteria or False otherwise
  321.   @type pred: callable
  322.   @return: Node matching the criteria or None if not found
  323.   @rtype: Accessibility.Accessible
  324.   '''
  325.     if acc is None:
  326.         return None
  327.     while acc.parent is None:
  328.         return None
  329.         acc is None
  330.         
  331.         try:
  332.             if pred(acc.parent):
  333.                 return acc.parent
  334.         except Exception:
  335.             pass
  336.  
  337.         acc = acc.parent
  338.         continue
  339.         return None
  340.  
  341.  
  342. def getPath(acc):
  343.     '''
  344.   Gets the path from the application ancestor to the given accessible in
  345.   terms of its child index at each level.
  346.   
  347.   @param acc: Target accessible
  348.   @type acc: Accessibility.Accessible
  349.   @return: Path to the target
  350.   @rtype: list of integer
  351.   @raise LookupError: When the application accessible cannot be reached
  352.   '''
  353.     path = []
  354.     while acc.parent is None:
  355.         path.reverse()
  356.         return path
  357.         
  358.         try:
  359.             path.append(acc.getIndexInParent())
  360.         except Exception:
  361.             raise LookupError
  362.  
  363.         acc = acc.parent
  364.         continue
  365.         return None
  366.  
  367.  
  368. class _StateSetImpl(Accessibility__POA.StateSet):
  369.     '''
  370.   Implementation of the StateSet interface. Clients should not use this class
  371.   directly, but rather the L{StateSet} proxy class.
  372.   
  373.   @param states: Set of states
  374.   @type states: set
  375.   '''
  376.     
  377.     def __init__(self):
  378.         '''Initializes the state set.'''
  379.         self.states = set()
  380.  
  381.     
  382.     def contains(self, state):
  383.         '''
  384.     Checks if this StateSet contains the given state.
  385.     
  386.     @param state: State to check
  387.     @type state: Accessibility.StateType
  388.     @return: True if the set contains the given state
  389.     @rtype: boolean
  390.     '''
  391.         return state in self.states
  392.  
  393.     
  394.     def add(self, state):
  395.         '''
  396.     Adds a state to this set.
  397.     
  398.     @param state: State to add
  399.     @type state: Accessibility.StateType
  400.     '''
  401.         self.states.add(state)
  402.  
  403.     
  404.     def remove(self, state):
  405.         '''
  406.     Removes a state from this set.
  407.     
  408.     @param state: State to remove
  409.     @type state: Accessibility.StateType
  410.     '''
  411.         self.states.remove(state)
  412.  
  413.     
  414.     def equals(self, state_set):
  415.         '''
  416.     Checks if this StateSet contains exactly the same members as the given
  417.     StateSet.
  418.     
  419.     @param state_set: Another set
  420.     @type state_set: Accessibility.StateSet
  421.     @return: Are the sets equivalent in terms of their contents?
  422.     @rtype: boolean
  423.     '''
  424.         return set(state_set.getStates()) == self.states
  425.  
  426.     
  427.     def compare(self, state_set):
  428.         """
  429.     Computes the symmetric differences of this L{StateSet} and the given
  430.     L{StateSet}.
  431.  
  432.     @note: This method is not currently implemented because of difficulties
  433.     with reference counting. This method needs to return a new
  434.     Accessibility.StateSet object, but the Python implementation for that
  435.     object needs to be kept alive. The problem is who will keep that
  436.     server implementation alive? As soon as it goes out of scope, it's
  437.     GC'ed. This object cannot keep it alive either as it may go out of
  438.     scope before the new object is ready to be finalized. With a global
  439.     cache of objects, we don't know when to invalidate.
  440.     
  441.     @param state_set: Another set
  442.     @type state_set: Accessibility.StateSet
  443.     @return: Elements in only one of the two sets
  444.     @rtype: Accessibility.StateSet
  445.     """
  446.         raise ORBit.CORBA.NO_IMPLEMENT
  447.  
  448.     
  449.     def isEmpty(self):
  450.         '''
  451.     Checks if this L{StateSet} is empty.
  452.     
  453.     @return: Is it empty?
  454.     @rtype: boolean
  455.     '''
  456.         return len(self.states) == 0
  457.  
  458.     
  459.     def getStates(self):
  460.         '''
  461.     Gets the sequence of all states in this set.
  462.     
  463.     @return: List of states
  464.     @rtype: list
  465.     '''
  466.         return list(self.states)
  467.  
  468.  
  469.  
  470. class StateSet(object):
  471.     '''
  472.   Python proxy for the L{_StateSetImpl} class. Use this to safely instantiate
  473.   new StateSet objects in Python.
  474.  
  475.   @param impl: State set implementation
  476.   @type impl: L{_StateSetImpl}
  477.   '''
  478.     
  479.     def __init__(self, *states):
  480.         '''
  481.     Initializes the state set with the given states.
  482.  
  483.     @param states: States to add immediately
  484.     @type states: list
  485.     '''
  486.         self.impl = _StateSetImpl()
  487.         map(self.impl._this().add, states)
  488.  
  489.     
  490.     def contains(self, state):
  491.         '''
  492.     Checks if this StateSet contains the given state.
  493.     
  494.     @param state: State to check
  495.     @type state: Accessibility.StateType
  496.     @return: True if the set contains the given state
  497.     @rtype: boolean
  498.     '''
  499.         return self.impl._this().contains(state)
  500.  
  501.     
  502.     def add(self, *states):
  503.         '''
  504.     Adds states to this set.
  505.     
  506.     @param states: State(s) to add
  507.     @type states: Accessibility.StateType
  508.     '''
  509.         map(self.impl._this().add, states)
  510.  
  511.     
  512.     def remove(self, state):
  513.         '''
  514.     Removes states from this set.
  515.     
  516.     @param states: State(s) to remove
  517.     @type states: Accessibility.StateType
  518.     '''
  519.         map(self.impl._this().remove, state)
  520.  
  521.     
  522.     def equals(self, state_set):
  523.         '''
  524.     Checks if this StateSet contains exactly the same members as the given
  525.     StateSet.
  526.     
  527.     @param state_set: Another set
  528.     @type state_set: Accessibility.StateSet
  529.     @return: Are the sets equivalent in terms of their contents?
  530.     @rtype: boolean
  531.     '''
  532.         if isinstance(state_set, self.__class__):
  533.             state_set = state_set.raw()
  534.         
  535.         return self.impl._this().equals(state_set)
  536.  
  537.     
  538.     def compare(self, state_set):
  539.         '''
  540.     Finds the symmetric difference between this state set andthe one provided,
  541.     and returns it as a new StateSet.
  542.  
  543.     @note: This does not use L{_StateSetImpl.compare} which cannot be
  544.     implemented at this time
  545.     @param state_set: Set to compare against
  546.     @type state_set: Accessibility.StateSet
  547.     @return: Proxy for the new set
  548.     @rtype: L{StateSet}
  549.     '''
  550.         if isinstance(state_set, self.__class__):
  551.             state_set = state_set.raw()
  552.         
  553.         a = set(self.impl._this().getStates())
  554.         b = set(state_set.getStates())
  555.         diff = a.symmetric_difference(b)
  556.         return StateSet(*diff)
  557.  
  558.     
  559.     def isEmpty(self):
  560.         '''
  561.     Checks if this StateSet is empty.
  562.     
  563.     @return: Is it empty?
  564.     @rtype: boolean
  565.     '''
  566.         return self.impl._this().isEmpty()
  567.  
  568.     
  569.     def getStates(self):
  570.         '''
  571.     Gets the sequence of all states in this set.
  572.     
  573.     @return: List of states
  574.     @rtype: list
  575.     '''
  576.         return self.impl._this().getStates()
  577.  
  578.     
  579.     def raw(self):
  580.         '''
  581.     Gets the Accessibility.StateSet object proxied for use in a remote
  582.     call.
  583.  
  584.     @return: State set
  585.     @rtype: Accessibility.StateSet
  586.     '''
  587.         return self.impl._this()
  588.  
  589.  
  590.